package com.cenxi.common.dto;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cenxi.common.base.BaseTree;
import com.cenxi.common.vo.MenuVO;
import com.cenxi.utils.TreeUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.tomcat.jni.Mmap;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author cenxi
 * @Date 2023/11/15 22:04
 * @Version 1.0
 */
//@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class App  {

    private Long id;

    private String name;

    /**
     * 父级id
     */
    private Long parentId;

    private List<App> children;

    public static void main(String[] args) {
        List<App> apps = new ArrayList<>();
        Collections.addAll(apps,
                new App(1L, "游戏", null, new ArrayList<>()),
                new App(2L, "游戏1", 1L, new ArrayList<>()),
                new App(3L, "游戏2", 2L, new ArrayList<>()),
                new App(4L, "电脑", null, new ArrayList<>()),
                new App(5L, "电脑1", 4L, new ArrayList<>()),
                new App(6L, "电脑2", 5L, new ArrayList<>()),
                new App(7L, "电脑3", 6L, new ArrayList<>()));
        apps = getChildren(apps);
        apps.forEach(System.out::println);
//        Map<String, List<Long>> respMap = new HashMap<>();
//        for (App app : apps) {
//            respMap.put(app.name, getAppIds(app, new ArrayList<>()));
//        }
//        System.out.println(JSON.toJSONString(respMap));
    }

    public static  List<App> getChildren(List<App> apps) {
        Map<Long, App> map = apps.stream().collect(Collectors.toMap(App::getId, a -> a));
        List<App> resList = new ArrayList<>();
        apps.forEach(v -> {
            if (Objects.isNull(v.getParentId())) {
                resList.add(map.get(v.getId()));
            } else {
                App app = map.get(v.getParentId());
                if (Objects.nonNull(app)) {
                    app.getChildren().add(v);
                } else {
                    resList.add(v);
                }
            }
        });
        return resList;
    }

    private static List<Long> getAppIds(App app, List<Long> respList) {
        respList.add(app.getId());
        List<App> children = app.getChildren();
        if (!CollectionUtils.isEmpty(children)) {
            for (App child : children) {
                getAppIds(child, respList);
            }
        }
        return respList;
    }

//    private List<String> getChnnelNames(List<Long> collect) {
//        List<String> names = new ArrayList<>();
//        if (!CollectionUtils.isEmpty(collect)) {
//            for (Long channelId : collect) {
//                names.add(getChannelName(channelId, new StringBuilder()));
//            }
//        }
//        return names;
//    }
//
//    private String getChannelName(Long id, StringBuilder s) {
//        ChannelInfo channelInfo = channelInfoMapper.selectById(id);
//        s.append(channelInfo.getChannelName());
//        if (Objects.nonNull(channelInfo.getParentId())) {
//            s.append("/");
//            getChannelName(channelInfo.getParentId(), s);
//        }
//        s.reverse();
//        return s.toString();
//    }

//    public Map<String, Integer> groupingByStreamCode() {
//        LocalDateTime now = LocalDateTime.now();
//        List<LiveTimeCalc> liveTimeCalcs = liveTimeCalcMapper.selectList(new LambdaQueryWrapper<LiveTimeCalc>()
//                .isNotNull(LiveTimeCalc::getTimeLength)
//                .between(LiveTimeCalc::getStartTime, now, now.minusWeeks(1)));
//        // list分组累加数据
//        List<LiveTimeCalc> respList;
//        Map<String, List<LiveTimeCalc>> collect = liveTimeCalcs.stream().collect(Collectors.groupingBy(LiveTimeCalc::getStreamCode));
//        respList = collect.keySet().stream().map(item -> {
//            LiveTimeCalc liveTimeCalc = collect.get(item).get(0);
//            liveTimeCalc.setTimeLength(collect.get(item).stream().mapToLong(LiveTimeCalc::getTimeLength).sum());
//            return liveTimeCalc;
//        }).collect(Collectors.toList());
//        respList = respList.stream().filter(item -> item.getTimeLength() >= 20).collect(Collectors.toList());
//        return anchorStatistics(respList);
//    }
//
//    public Map<String, Integer> anchorStatistics(List<LiveTimeCalc> liveTimeCalcs) {
//        List<ChannelInfo> channelInfos = channelInfoMapper.selectList(new LambdaQueryWrapper<ChannelInfo>()
//                .eq(ChannelInfo::getEnabled, Boolean.TRUE));
//        List<ChannelInfoVO> result = BeanUtil.copyList(channelInfos, ChannelInfoVO.class);
//        result = TreeUtil.parseTree(result);
//        Map<String, List<Long>> map = new HashMap<>();
//        result.forEach(item -> map.put(item.getChannelName(), anchorChannelRelMapper.selectList(new LambdaQueryWrapper<AnchorChannelRel>()
//                        .in(AnchorChannelRel::getChannelId, getChannelIds(item, new ArrayList<>())))
//                .stream().map(AnchorChannelRel::getAnchorId).collect(Collectors.toList())));
//        List<String> liveTimeCalcCode = liveTimeCalcs.stream().map(LiveTimeCalc::getStreamCode).collect(Collectors.toList());
//        List<Long> anchorIds = anchorMapper.selectList(new LambdaQueryWrapper<Anchor>()
//                .in(Anchor::getStreamCode, liveTimeCalcCode)
//                .eq(Anchor::getEnabled, Boolean.TRUE)).stream().map(Anchor::getId).collect(Collectors.toList());
//        Map<String, Integer> respMap = new HashMap<>();
//        map.keySet().forEach(item -> respMap.put(item, getAnchorNum(anchorIds, map.get(item))));
//        return respMap;
//    }
//
//    private Integer getAnchorNum(List<Long> anchorIds, List<Long> longs) {
//        int num = 0;
//        for (Long anchorId : anchorIds) {
//            if (longs.contains(anchorId)) {
//                num += 1;
//            }
//        }
//        return num;
//    }

}
